package com.airsystem.pos.bengkel.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import com.airsystem.pos.bengkel.entity.DetailPelanggan;
import com.airsystem.pos.bengkel.entity.Pelanggan;
import com.airsystem.pos.bengkel.util.HibernateUtil;

/**
 * @author Budi Oktaviyan <budi.oktaviyan@icloud.com>
 */
public class PelangganServices {

	/* GENERIC DAO */
	public void saveOrUpdate(Pelanggan pelanggan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.saveOrUpdate(pelanggan);
		session.flush();
		session.close();
	}

	public void saveOrUpdate(DetailPelanggan detailPelanggan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.saveOrUpdate(detailPelanggan);
		session.flush();
		session.close();
	}

	public void delete(Pelanggan pelanggan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.delete(pelanggan);
		session.flush();
		session.close();
	}

	public void delete(DetailPelanggan detailPelanggan) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.delete(detailPelanggan);
		session.flush();
		session.close();
	}

	public List<DetailPelanggan> select() throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();

		Criteria criteria = session.createCriteria(DetailPelanggan.class);
		criteria.createAlias("pelanggan", "pelanggan");
		criteria.createAlias("detail_kendaraan", "detail_kendaraan");
		criteria.createAlias("detail_kendaraan.kendaraan", "kendaraan");

		criteria.addOrder(Order.asc("pelanggan.nama"));
		criteria.addOrder(Order.asc("kendaraan.merk"));
		criteria.addOrder(Order.asc("detail_kendaraan.sub_merk"));

		List<?> results = criteria.list();
		List<DetailPelanggan> detailPelanggans = new ArrayList<DetailPelanggan>();

		for (Object result : results) {
			DetailPelanggan detailPelanggan = (DetailPelanggan) result;
			detailPelanggans.add(detailPelanggan);
		}

		return detailPelanggans;
	}

	/* CUSTOM DAO */
	public List<DetailPelanggan> select(String searchCombo, String searchText) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();

		StringBuffer QUERY = new StringBuffer("FROM DetailPelanggan WHERE ");
		QUERY.append(searchCombo);
		QUERY.append(" LIKE:LIKE ORDER BY pelanggan.nama ASC");

		StringBuffer LIKE = new StringBuffer("%");
		LIKE.append(searchText);
		LIKE.append("%");

		List<?> query = (List<?>) session.createQuery(QUERY.toString()).setString("LIKE", LIKE.toString()).list();
		session.close();

		List<DetailPelanggan> detailPelanggans = new ArrayList<DetailPelanggan>();
		for (Object result : query) {
			DetailPelanggan detailPelanggan = (DetailPelanggan) result;
			detailPelanggans.add(detailPelanggan);
		}

		return detailPelanggans;
	}

	public List<DetailPelanggan> select(String nama) throws Exception {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<?> query = (List<?>) session.createQuery("FROM DetailPelanggan WHERE pelanggan.nama=:nama").setString("nama", nama).list();
		session.close();

		List<DetailPelanggan> detailPelanggans = new ArrayList<DetailPelanggan>();
		for (Object result : query) {
			DetailPelanggan detailPelanggan = (DetailPelanggan) result;
			detailPelanggans.add(detailPelanggan);
		}

		return detailPelanggans;
	}
}